home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / NewsBase / Source / ITreeNodeD.m < prev    next >
Text File  |  1993-01-12  |  4KB  |  143 lines

  1. #import "ITreeNodeD.h"
  2. #import "IDatumD.h"
  3. #import <stdio.h>
  4. #import <appkit/Application.h>
  5. #import "errdebug.h"
  6.  
  7. @implementation ITreeNodeD
  8.  
  9. - (ITreeNodeD *)nodeForKey:(char **)theKey
  10. {
  11.     return [self searchForNodeWithKey:theKey withOption:SEARCH];
  12. }
  13.  
  14. - addNodeForKey:(char **)theKey
  15. {
  16.     return [self searchForNodeWithKey:theKey withOption:ADD];
  17. }
  18.  
  19. //- (void)setLeaf:aLeaf
  20. //{
  21. //    id datum;
  22. //
  23. //    if ((datum = [self dataForKey:"leaf"]) != nil) {
  24. //        [datum setData:aLeaf];
  25. //    } else {
  26. //        [self insertKeyedObject:[[IDatumD allocFromZone:[self zone]] initWithKey:"leaf" andData:aLeaf]];
  27. //    }
  28. //}
  29. //- leaf
  30. //{
  31. //    return [[self dataForKey:"leaf"] data];
  32. //}
  33.  
  34. //- (void)setLink:aLink;
  35. //{
  36. //    id datum;
  37. //
  38. //    if ((datum = [self dataForKey:"link"]) != nil) {
  39. //        [datum setData:aLink];
  40. //    } else {
  41. //        [self insertKeyedObject:[[IDatumD allocFromZone:[self zone]] initWithKey:"link" andData:aLink]];
  42. //    }
  43. //}
  44.  
  45. //- link
  46. //{
  47. //    return [[self dataForKey:"link"] data];
  48. //}
  49.  
  50. - searchForNodeWithKey:(char **)theKey withOption:(int)opCode
  51. {
  52.     ITreeNodeD *node;
  53.  
  54.     
  55.     if (*theKey == 0) {
  56.         return self;
  57.     }
  58.  
  59.     if ((node = [self dataForKey:*theKey]) == nil) {
  60.         if (opCode == SEARCH) {
  61.             return nil;
  62.         } else if (opCode == ADD) {
  63.             node = [[ITreeNodeD allocFromZone:[self zone]] initWithKey: *theKey];
  64.             [self insertKeyedObject:node];
  65. //        [node setLink:nil];
  66. //        [node setLeaf:nil];
  67.             return [node searchForNodeWithKey:(theKey + 1) withOption:opCode];
  68.         }
  69.     } else if ([node isMemberOf:[ITreeNodeD class]] == YES) {
  70.         return [node searchForNodeWithKey:(theKey + 1) withOption:opCode];
  71.     } else {
  72.         if (*(theKey + 1) == 0) {
  73.             return node;
  74.         } else {
  75.             return nil;
  76.         }
  77.     }
  78.     return nil;  /* need to fool the compiler */
  79. }
  80.  
  81. - (void)traverseTreeAndPerform:(SEL)theSelector target:target
  82. {
  83.     int n;
  84.     ITreeNodeD *node;
  85.  
  86.     [target perform:theSelector with:self];
  87.     for (n = 0; node = [self objectAt:n]; ++n) {
  88.         if ([node isMemberOf:[ITreeNodeD class]] == YES) {
  89.             [node traverseTreeAndPerform:theSelector target:target];
  90.         }
  91.     }
  92. }
  93.  
  94. - (void)initState:(NXTreeState *)state
  95. {
  96.     state->depth = 0;
  97.     state->nodeState[0].node = self;
  98.     state->nodeState[0].index = 0;
  99. }
  100.  
  101. - nextState:(NXTreeState *)state
  102. {
  103.     id node;
  104.  
  105.     DBG(10, fprintf(stderr, "%s %d\n", [state->nodeState[state->depth].node key], state->nodeState[state->depth].index););
  106.     if ((node = [state->nodeState[state->depth].node objectAt:state->nodeState[state->depth].index]) != nil) {
  107.         ++state->nodeState[state->depth].index;
  108.         if ([node isMemberOf:[ITreeNodeD class]] == YES && [node count] > 0) {
  109.         if (++state->depth > MAX_TREE_STATE_DEPTH) {
  110.         [NXApp terminate:self];
  111.             }
  112.             state->nodeState[state->depth].node = node;
  113.             state->nodeState[state->depth].index = 0;
  114.         }
  115.         return(node);
  116.     } else {
  117.         if (--state->depth >= 0) {
  118.             return [self nextState:state];
  119.         } else {
  120.             return nil;
  121.         }
  122.     }
  123. }
  124.  
  125. #import <sys/param.h>
  126. #import <libc.h>
  127. void makeTreeKey(const char *originalKey, const char **key, const char *token)
  128. {
  129.     char    **keyptr;
  130.     char    *strtok();
  131.     char    *keytmp;
  132.     
  133.     keytmp = (char *)originalKey;
  134.     keyptr = key;
  135.     *keyptr = strtok(keytmp, token);
  136.     while (++keyptr < &key[MAX_NO_OF_TOKENS -1] &&
  137.             (*keyptr = strtok(NULL, token)) != NULL );
  138.     *++keyptr = NULL;
  139.     return;
  140. }
  141.  
  142. @end
  143.